home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
intrvews
/
xgrab.lha
/
xgrab
/
grabst
/
token.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-06
|
7KB
|
356 lines
/**
GRAB Graph Layout and Browser System
Copyright (c) 1989, Tera Computer Company
**/
/**
token.c is a scanner, specialized for two uses: reading graph
input files, and reading predicate files.
The difference manifests itself in two places: the graph files don't
care about reserved words, and the graph files don't care about special
characters in strings. So, when reading graph files, get_next_token
should be invoked with both boolean arguments FALSE, and when reading
predicate files, both arguments should be TRUE.
**/
#include <ctype.h>
#include <stdio.h>
#include "attribute.h"
#include "digraph.h"
#include "token.h"
/* reserved words */
char *reserved_wd[NUM_RESERVED] =
{
"and",
"or",
"not",
"if",
"then",
"elsif",
"else",
"endif",
"break",
"continue",
"set",
"coalesce",
"expand",
"hide",
"display",
"focus",
"source",
"sink",
"in",
"out",
"parents",
"children",
"ancestors",
"descendants",
"shape",
"brush",
"color",
"displayed",
"node",
"edge",
"coalescer"
};
static char lastc; /* the last character we read. We need one character
of lookahead, and rather than pushing it back
on the stream, we just keep it around. */
char char_val; /* character value for illegal characters */
char string_val[MAXBUFFER]; /* string value for strings and quoted strings */
FILE *readfile; /* input file */
TOKEN token_val();
init_reader(rfile)
FILE *rfile;
{
readfile = rfile;
lastc = ' ';
}
TOKEN get_next_token(reserved, str_restricted)
BOOL reserved; /* if not TRUE, treat reserved words as strings */
BOOL str_restricted; /* if TRUE, strings cannot contain special characters */
{
char_val = '\0';
string_val[0] = '\0';
while (lastc != EOF && whitespace(lastc))
/* skip whitespace (tabs and spaces) */
{
lastc = getc(readfile);
}
if (lastc == EOF) /* eof */
{
return EOF_TOKEN;
}
else if (lastc == '\n') /* newline */
{
lastc = ' '; /* common idiom: if we didn't use lookahead,
set lastc to a space, which will not affect
the scanner when it is next invoked */
return NEWLINE_TOKEN;
}
else if (!isprint(lastc)) /* illegal character */
{
char_val = lastc;
lastc = ' ';
return ILLEGAL_CHAR_TOKEN;
}
else if (!endchar(lastc) && lastc != '\"' &&
(!special(lastc) || (!str_restricted)))
/* read a string */
{
read_tstring(string_val, readfile, MAXBUFFER, str_restricted);
if (!reserved)
/* don't care about reserved words */
{
return STRING_TOKEN;
}
else
{
return token_val(string_val);
}
}
else
/* special character (we hope) */
{
TOKEN t;
switch (lastc)
{
case '=':
t = EQUAL_TOKEN;
break;
case '.':
t = PERIOD_TOKEN;
break;
case '(':
t = OPENPAREN_TOKEN;
break;
case ')':
t = CLOSEPAREN_TOKEN;
break;
case ';':
t = SEMICOLON_TOKEN;
break;
case '\"':
read_quoted_string(string_val, readfile, MAXBUFFER);
return QSTRING_TOKEN;
default:
char_val = lastc;
t = ILLEGAL_START_TOKEN;
break;
}
lastc = ' ';
return t;
}
}
read_quoted_string (s, readfile, limit)
char s[];
FILE *readfile;
int limit;
/**
we've read a quotation mark, now read a quoted string from readfile.
limit is the length of s
**/
{
int i = 0;
if (limit <= 1)
{
fprintf(stderr, "read_quoted_string: limit %d too small\n", limit);
return;
}
while ((lastc = getc(readfile)) != EOF &&
(whitespace(lastc) || isprint(lastc)) && lastc != '\n')
{
if (lastc == '\"')
/* end of string */
{
s[i] = '\0';
lastc = ' ';
return;
}
else if (lastc == '\\')
/* special character marker */
{
if ((lastc = getc(readfile)) == EOF || !isprint(lastc))
{
char_val = lastc;
print_bad_char(
"read_quoted_string: Aborted due to illegal character");
s[i++] = '\\';
s[i] = '\0';
return;
}
else
{
s[i++] = lastc;
}
}
else
/* normal character */
{
s[i++] = lastc;
}
if (i == limit - 1)
{
fprintf(stderr, "quoted string length limit reached\n");
s[i] = '\0';
/* check if the string was just finished, anyway */
if ((lastc = getc(readfile)) == '\"')
{
lastc = ' ';
}
return;
}
}
char_val = lastc;
print_bad_char("read_quoted_string: Aborted due to illegal character");
s[i] = '\0';
return;
}
read_tstring (s, readfile, limit, str_restricted)
char s[];
FILE *readfile;
int limit;
BOOL str_restricted; /* restricted forms of strings */
/**
read a text string from readfile. limit is the length of s.
if str_restricted is true, special characters aren't allowed in
the string
**/
{
int i = 0;
if (limit <= 1)
{
fprintf(stderr, "read_tstring: limit %d too small\n", limit);
return;
}
do
{
s[i++] = lastc;
if (i == limit - 1)
{
fprintf(stderr, "string length limit reached\n");
break;
}
lastc = getc(readfile);
} while (!endchar(lastc) && (!special(lastc) || !str_restricted));
s[i] = '\0';
}
TOKEN token_val(s)
char *s;
/**
if s is a reserved word, return token value of s. Else, return the
token for a string.
**/
{
int i;
for (i = 0; i < NUM_RESERVED; i++)
{
if (!strcmp(s, reserved_wd[i]))
{
return ((TOKEN) (i + (int) FIRST_RESERVED_TOKEN));
}
}
return STRING_TOKEN;
}
print_token(t, nl)
TOKEN t;
BOOL nl; /* print a newline or not? */
/* print a text version of t, followed by a newline if nl is TRUE */
{
if ((int) t >= (int) FIRST_RESERVED_TOKEN &&
(int) t - (int) FIRST_RESERVED_TOKEN < NUM_RESERVED)
{
fprintf(stderr, "reserved \`%s\'",
reserved_wd[(int) t - (int) FIRST_RESERVED_TOKEN]);
}
else
{
switch(t)
{
case EOF_TOKEN:
fprintf(stderr, "eof");
break;
case ILLEGAL_CHAR_TOKEN:
print_bad_char("illegal char");
break;
case ILLEGAL_START_TOKEN:
print_bad_char("illegal start");
break;
case STRING_TOKEN:
fprintf(stderr, "string \`%s\'", string_val);
break;
case QSTRING_TOKEN:
fprintf(stderr, "quoted string \`%s\'", string_val);
break;
case NEWLINE_TOKEN:
fprintf(stderr, "newline");
break;
case EQUAL_TOKEN:
fprintf(stderr, "equal");
break;
case PERIOD_TOKEN:
fprintf(stderr, "period");
break;
case OPENPAREN_TOKEN:
fprintf(stderr, "open paren");
break;
case CLOSEPAREN_TOKEN:
fprintf(stderr, "close paren");
break;
case SEMICOLON_TOKEN:
fprintf(stderr, "semicolon");
break;
default:
fprintf(stderr, "illegal token %d", t);
break;
}
}
if (nl)
{
fprintf(stderr, "\n");
}
}
print_bad_char(s)
char *s;
/* print an illegal character in a pseudo-readable form */
{
if (isprint(char_val))
{
fprintf (stderr, "%s: \`%c\'\n", s, char_val);
}
else
{
fprintf (stderr, "%s: %.3o\n", s, char_val);
}
}